package com.xbk.seckill.activity.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.xbk.seckill.activity.application.command.SeckillActivityCommand;
import com.xbk.seckill.activity.application.service.SeckillActivityService;
import com.xbk.seckill.activity.domain.model.SeckillActivity;
import com.xbk.seckill.common.exception.ErrorCode;
import com.xbk.seckill.common.model.dto.SeckillActivityDTO;
import com.xbk.seckill.common.response.ResponseMessage;
import com.xbk.seckill.common.response.ResponseMessageBuilder;
import com.xbk.seckill.common.utils.date.JodaDateTimeUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 活动Controller
 *
 * @author xiexu
 * @date 2025/1/11 23:05
 */
@RestController
@RequestMapping(value = "/activity")
public class SeckillActivityController {

    @Resource
    private SeckillActivityService seckillActivityService;

    /**
     * 保存秒杀活动
     */
    @RequestMapping(value = "/saveSeckillActivity", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMessage<String> saveSeckillActivityDTO(@RequestBody SeckillActivityCommand seckillActivityCommand) {
        seckillActivityService.saveSeckillActivityDTO(seckillActivityCommand);
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode());
    }

    /**
     * 根据状态获取活动列表
     */
    @RequestMapping(value = "/getSeckillActivityList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMessage<List<SeckillActivity>> getSeckillActivityList(@RequestParam(value = "status", required = false) Integer status) {
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), seckillActivityService.getSeckillActivityList(status));
    }

    /**
     * 获取秒杀活动列表
     *
     * @param status  状态
     * @param version 版本号
     */
    @RequestMapping(value = "/seckillActivityList", method = {RequestMethod.GET, RequestMethod.POST})
    @SentinelResource(value = "QUEUE-DATA-FLOW")
    public ResponseMessage<List<SeckillActivityDTO>> getSeckillActivityList(@RequestParam(value = "status", required = false) Integer status, @RequestParam(value = "version", required = false) Long version) {
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), seckillActivityService.getSeckillActivityList(status, version));
    }

    /**
     * 通过活动id获取秒杀活动详情
     *
     * @param id      活动id
     * @param version 版本号
     */
    @RequestMapping(value = "/seckillActivity", method = {RequestMethod.GET, RequestMethod.POST})
    @SentinelResource(value = "QUEUE-DATA-FLOW")
    public ResponseMessage<SeckillActivityDTO> getSeckillActivityById(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "version", required = false) Long version) {
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), seckillActivityService.getSeckillActivity(id, version));
    }

    /**
     * 根据时间和状态获取活动列表
     */
    @RequestMapping(value = "/getSeckillActivityListBetweenStartTimeAndEndTime", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMessage<List<SeckillActivity>> getSeckillActivityListBetweenStartTimeAndEndTime(@RequestParam(value = "currentTime", required = false) String currentTime, @RequestParam(value = "status", required = false) Integer status) {
        List<SeckillActivity> seckillActivityList = seckillActivityService.getSeckillActivityListBetweenStartTimeAndEndTime(JodaDateTimeUtils.parseStringToDate(currentTime, JodaDateTimeUtils.DATE_TIME_FORMAT), status);
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), seckillActivityList);
    }

    /**
     * 根据活动id获取秒杀活动详情
     */
    @RequestMapping(value = "/getSeckillActivityById", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMessage<SeckillActivity> getSeckillActivityById(@RequestParam(value = "id", required = false) Long id) {
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), seckillActivityService.getSeckillActivityById(id));
    }

    /**
     * 更新活动的状态
     */
    @RequestMapping(value = "/updateStatus", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMessage<String> updateStatus(@RequestParam(value = "status", required = false) Integer status, @RequestParam(value = "id", required = false) Long id) {
        seckillActivityService.updateStatus(status, id);
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode());
    }
}
